/**
 * 
 */
package org.agiba.xwiki.macro.jira;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.agiba.xwiki.macro.jira.internal.JiraInstance;
import org.xwiki.properties.annotation.PropertyDescription;
import org.xwiki.properties.annotation.PropertyMandatory;
import org.xwiki.properties.annotation.PropertyName;
import org.xwiki.rendering.macro.parameter.MacroParameterException;

/**
 * Parameterdefinitions for the JIRA-Shortcut.
 * 
 * @author Thorsten Kamann <thorsten.kamann@googlemail.com>
 * @since 1.0.0
 */
public class JiraShortcutMacroParameters {
	private String issue;
	private String instance;

	public static Map<String, JiraInstance> JIRA_INSTANCES = new HashMap<String, JiraInstance>();
	protected static String configFileName = "xwiki.macro.jira.properties";

	/**
	 * Creates a new instance of this class. After creation it checks if the Jira instances
	 * already configured. If no the properties will be read and the instances set.
	 * 
	 * @throws MacroParameterException If no configuration found
	 */
	public JiraShortcutMacroParameters() throws MacroParameterException {
		if (JIRA_INSTANCES.isEmpty()) {
			readConfiguration();
		}
	}

	/**
	 * Reads the configuration out of the properties file. Checks the default server too.
	 * 
	 * @throws MacroParameterException
	 */
	private void readConfiguration() throws MacroParameterException {
		try {
			Properties properties = new Properties();
			properties.load(JiraShortcutMacroParameters.class
					.getResourceAsStream("/" + configFileName));
			for (@SuppressWarnings("rawtypes")
			Iterator iter = properties.keySet().iterator(); iter.hasNext();) {
				String name = (String) iter.next();
				String url = properties.getProperty(name);
				boolean _default = false;
				if (name.endsWith("*")) {
					_default = true;
					name = name.substring(0, name.length() - 1);
				}
				JiraInstance ji = new JiraInstance(name, url, _default);
				JIRA_INSTANCES.put(name, ji);
			}
		} catch (Exception e) {
			throw new MacroParameterException(
					"The configuration file 'xwiki.macro.jira.properties' was not found.",
					e);
		}
	}

	/**
	 * @return the JIRA-Instance used for this instance of the JiraShortcut macro
	 * @throws MacroParameterException
	 */
	public String getInstance() throws MacroParameterException {
		if (JIRA_INSTANCES == null || JIRA_INSTANCES.isEmpty()) {
			throw new MacroParameterException(
					"The JIRA-Instances was configured.");
		}
		if (instance == null || instance.equals("")) {
			for (JiraInstance ji : JIRA_INSTANCES.values()) {
				if (ji._default) {
					instance = ji.name;
					break;
				}
			}

			if (instance == null || instance.equals("")) {
				if (JIRA_INSTANCES.size() < 2) {
					Map.Entry<String, JiraInstance> entry = JIRA_INSTANCES
							.entrySet().iterator().next();
					instance = ((JiraInstance) entry.getValue()).name;
				} else {
					throw new MacroParameterException(
							"More than one JIRA-Instances and no default instance was configured.");
				}
			}
		}
		return instance;
	}

	/**
	 * @param jiraInstance
	 *            the jiraInstance to set
	 */
	@PropertyDescription("The Jira instance to create the shortcut for")
	@PropertyName("instance")
	public void setInstance(String instance) {
		this.instance = instance;
	}

	/**
	 * @return the issue
	 */
	public String getIssue() {
		return issue;
	}

	/**
	 * The issue parameter:
	 * <ul>
	 * <li>Here you can enter an issue key, eg. FRT-124</li>
	 * </ul>
	 * 
	 * @param the
	 *            issue to create tje JIRA-link for
	 */
	@PropertyDescription("The issue to create the shortcut for")
	@PropertyMandatory
	public void setIssue(String issue) {
		this.issue = issue;
	}

}
